package com.atguigu.statistics.service.impl;

import com.atguigu.commonutils.RandomUtil;
import com.atguigu.commonutils.Result;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.statistics.client.UcenterClient;
import com.atguigu.statistics.entity.Daily;
import com.atguigu.statistics.mapper.DailyMapper;
import com.atguigu.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-07-12
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    UcenterClient ucenterClient;
    //日期格式的限制
    private static final Pattern DATE_PATTERN = Pattern.compile("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
    @Override
    public void registerCount(String day) {
        if(!DATE_PATTERN.matcher(day).matches()){
            //抛出异常
            throw new GuliException(20001,"请输入日期格式：yyyy-mm-dd");
        }


        //删除表中相同的日期数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        if (baseMapper.selectCount(wrapper)>0){
            baseMapper.delete(wrapper);
        }


        //远程调用得到某一天的注册人数
        Result result = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) result.getData().get("countRegister");

        //把获取数据添加导数据库中，统计分析表面
        Daily daily = new Daily();
        daily.setRegisterNum(countRegister);//每日注册人数
        daily.setDateCalculated(day);//统计日期
        //TODO:模拟场景，并非实际，后续代码进行补充
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));//每日播放视频数
        daily.setLoginNum(RandomUtils.nextInt(100,200));//每日登录人数
        daily.setCourseNum(RandomUtils.nextInt(100,200));//每日新增课程数
        baseMapper.insert(daily);

    }
    //图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        List<Daily> dailyList = baseMapper.selectList(wrapper);
        List<String> dataCalculatedList = dailyList.stream().map(Daily::getDateCalculated).collect(Collectors.toList());
        List<Integer> numDataList = new ArrayList<>();
        dailyList
                .forEach(data->{
                    switch (type){
                        case "login_num":
                            numDataList.add(data.getLoginNum());
                            break;
                        case "register_num":
                            numDataList.add(data.getRegisterNum());
                            break;
                        case "video_view_num":
                            numDataList.add(data.getVideoViewNum());
                            break;
                        case "course_num":
                            numDataList.add(data.getCourseNum());
                            break;
                    }
                });


        Map<String, Object> map = new HashMap<>();
        map.put("dataCalculatedList",dataCalculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
